function [err]=mat2datac(filename, voltage, current, dfile)
%function to write datac files from the voltage and current.....
%writes all of voltage and current to the DATAC-file <filename>
% as one file 12/22/98 P. Manis
QueMessage(sprintf('Writing datac file %s', filename));

% Note: this version writes MODE 9 data, in which the current and voltage
% are stored as floating point values in mV and pA

err=0;
endian='ieee-le'; %data from little endian (i.e. PC's) => integers flipped, floats in ieee
dsize = size(voltage);
RL=[1:dsize(1)];
% initialize dfile - any arguments that are in dfile must be defined here....
% but only initialize it if it is empty coming in. 
if(isempty(dfile))
    dfile.filename=filename;
    dfile.fullfile='';
    dfile.path='';
    dfile.ext='';
    dfile.records_in_file = dsize(1);
    dfile.nr_points=dsize(2);
    dfile.comment='';
    dfile.mode=9;
    dfile.dmode = 'CC'; % default data collection mode if not otherwise defined...
    % dfile.rate = 0;
    dfile.ftime=0;
    dfile.record=0;
    dfile.nr_channel=2;
    dfile.junctionpot = 0;
    dfile.vgain=1;
    dfile.wgain=1;
    dfile.igain=1;
    dfile.gain=zeros(length(RL),8);
    dfile.low_pass=zeros(length(RL),8);
    dfile.slow=zeros(length(RL));
    dfile.ztime=zeros(length(RL));
    dfile.refvg=1;
    dfile.refig=1;
    dfile.refwg=1;
    dfile.frec=1;
    dfile.lrec=1;
end;

data=zeros(2,dsize(2)); % initialize to make rest of program happy -

fid=fopen(filename,'w',endian);
if fid ==-1
   QueMessage(sprintf('File %s not found ',filename));
   err=1;
   return
end

%write file header

fseek(fid, 0, -1);
fwrite(fid,dfile.nr_points,'int16');
blank=zeros(27,1);
fwrite(fid,blank,'char');
%blank2=zeros(100,1);
if(length(dfile.comment) > 80)
    blank2 = dfile.comment(1:80); % truncate comment to 80 characters
    ml = 80;
else
    blank2 = dfile.comment;
    ml = length(dfile.comment);
end;
blank2a = zeros(100-ml, 1);
fwrite(fid,blank2,'int8');
fwrite(fid,blank2a, 'int8');
ftell(fid)
% now for each reocrd, write a record header and the data
blank3 = zeros(256,1);
nrec = size(voltage)
datasp = 4; % 32 bit floating point numbers are 4 bytes.
for rec=1:nrec(1)
   base_offset=floor(128+(rec-1)*(datasp*dfile.nr_points*dfile.nr_channel+256)); %jump to record header start
   fseek(fid,base_offset,-1); % go to the right location
   fwrite(fid, blank3, 'int8');
   fseek(fid, base_offset, -1);
 %  ferror(fid)
%   z=ftell(fid)
   fwrite(fid,dfile.mode,'char');
   fwrite(fid,dfile.ftime,'char');
   fwrite(fid,rec,'int16'); %should be 1 for first record
   fwrite(fid,dfile.nr_channel,'int16');
   fwrite(fid, dfile.rate, 'float32');
%   dfile.nr_channel;
   data_offset=floor(256+base_offset); %jump to data start
   s=fseek(fid,data_offset,-1);	%skip earlier records
 %  ferror(fid)
 %  a=ftell(fid)
   data(1:2:2*dfile.nr_points-1) =voltage(rec,1:dfile.nr_points);
   data(2:2:2*dfile.nr_points)   = current(rec,1:dfile.nr_points);
   s=fwrite(fid,data,'float32');
   ferror(fid);
   b=ftell(fid);
end 
fclose(fid);
QueMessage(sprintf('Wrote file'));
return 
